package cn.hx.mall.sell.controller;

import cn.hx.mall.sell.entity.Coupon;
import cn.hx.mall.sell.entity.SmsCouponHistory;
import cn.hx.mall.sell.service.CouponService;
import cn.hx.mall.sell.service.SmsCouponHistoryService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 优惠券表(Coupon)表控制层
 *
 * @author makejava
 * @since 2022-11-02 16:10:27
 */
@Controller
@RequestMapping("sell")
public class CouponController {
    /**
     * 服务对象
     */
    @Resource
    private CouponService couponService;
    @Resource
    private SmsCouponHistoryService historyService;

    //查询全部
    @GetMapping("coupon_index")
    public String selectAll(Model model ,@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "5") Integer size) {
        Page<Coupon> coupons = PageHelper.startPage(page, size);
        couponService.selectAll();
        Date date = new Date();
        model.addAttribute("Time", date);
        PageInfo<Coupon> pageInfo = new PageInfo<>(coupons);
        model.addAttribute("coupon",pageInfo);
        return "sell/coupon_index";
    }

    //    添加和修改页面跳转+回显
    @GetMapping("coupon_edit")
    public String coupon_edit( Integer id,Model model) {
        Coupon coupon = couponService.queryById(id);
        if(id!=null){
            model.addAttribute("coupon",coupon);
        }
        return "sell/coupon_edit";
    }

    //    删除
    @GetMapping("coupon_delete")
    public String delete(Integer id) {
        couponService.deleteById(id);
        return "redirect:/sell/coupon_index";
    }

    //  添加或修改
    @PostMapping("edit")
    public String edit(Integer id,Coupon coupon) {
       if(id==null){
           couponService.insert(coupon);
       }else {
           couponService.update(coupon);
       }
        return "redirect:/sell/coupon_index";
    }
    //编辑页面重置
    @GetMapping("coupon_restart")
    public String restart() {
        return "sell/coupon_edit";
    }

    //查看页面跳转和显示数据
    @GetMapping("coupon_select")
    public String coupon_select(Model model,Integer id,@RequestParam(defaultValue = "1") Integer page) {
        Page<Coupon> coupons = PageHelper.startPage(page,5);
       historyService.selectAll();
        Coupon coupon = couponService.queryById(id);
        PageInfo<Coupon> pageInfo = new PageInfo<>(coupons);
        Date date = new Date();
        model.addAttribute("Time",date);
        model.addAttribute("coupon",coupon);
        model.addAttribute("histories",pageInfo);
        return "sell/coupon_select";
    }
    //首页模糊搜索
    @PostMapping("selectById")
    @ResponseBody
    public PageInfo<Coupon> selectByLike(Coupon coupon,@RequestParam(defaultValue = "1") Integer page){
        Page<Coupon> coupons = PageHelper.startPage(page, 5);
        couponService.queryAll(coupon);
        return new PageInfo<>(coupons);
    }
    //查看页面模糊搜索
    @PostMapping("queryById")
    @ResponseBody
    public PageInfo<SmsCouponHistory>queryById(SmsCouponHistory smsCouponHistory,@RequestParam(defaultValue = "1") Integer page){
        Page<SmsCouponHistory> pageInfo = PageHelper.startPage(page, 5);
        historyService.queryAll(smsCouponHistory);
        return new PageInfo<>(pageInfo);
    }
    //查看页面重置
    @GetMapping("select_restart")
    public String select_restart() {
        return "redirect:/sell/coupon_select";
    }
    //首页重置
    @GetMapping("index_restart")
    public String index_restart() {
        return "redirect:/sell/coupon_index";
    }
}